home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 June / SGI Freeware 1998 June.iso / dist / fw_UMINNgopher.idb / usr / freeware / src / gopher_1.12 / gopherd / openers.c.z / openers.c
C/C++ Source or Header  |  1997-09-09  |  4KB  |  202 lines

  1. /********************************************************************
  2.  * $Author: drich $
  3.  * $Revision: 1.1 $
  4.  * $Date: 1995/10/03 04:08:38 $
  5.  * $Source: /proj/freeware1.0/gopher1.12/src/gopherd/RCS/openers.c,v $
  6.  * $Status: $
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: openers.c
  14.  * See below
  15.  *********************************************************************
  16.  * Revision History:
  17.  * $Log: openers.c,v $
  18.  * Revision 1.1  1995/10/03  04:08:38  drich
  19.  * gopher 1.2 check-in
  20.  *
  21.  * Revision 1.1  1992/12/10  23:13:27  lindner
  22.  * gopher 1.1 release
  23.  *
  24.  *
  25.  *********************************************************************/
  26.  
  27.  
  28.  
  29. /*
  30.  * Routines that implement safe "openers" so that we can do without
  31.  * the chroot().  This is an advantage because then you can have
  32.  * symbolic links from your gopher server directory to other files
  33.  * that are elsewhere on your system, without (if we've done this right)
  34.  * compromising your security, or allowing access to any files that
  35.  * you don't want made available.
  36.  *
  37.  * The "r" in the names is meant to indicate "restricted".
  38.  * The "u" in the names is meant to indicate "unrestricted".
  39.  */
  40.  
  41. #include "gopherd.h"
  42. #include <sys/param.h>    /* for MAXPATHLEN */
  43.  
  44. /* and restore our real names */
  45. #undef open
  46. #undef fopen
  47. #undef stat
  48. #undef opendir
  49. #undef chdir
  50.  
  51.  
  52. char    *fixfile();
  53.  
  54.  
  55. int
  56. ropen( path, flags, mode )
  57. char *path;
  58. int flags, mode;
  59. {
  60.      char *p;
  61.      p = fixfile(path);
  62.      if (p != NULL)
  63.       return( open( p, flags, mode ) );
  64.      return(-1);    /* failed */
  65. }
  66.  
  67.  
  68. FILE *
  69. rfopen( filename, type )
  70. char *filename, *type;
  71. {
  72.      char *p;
  73.      p = fixfile(filename);
  74.      if (p != NULL)
  75.       return( fopen( p, type ) );
  76.      return(NULL);    /* failed */
  77. }
  78.  
  79.  
  80. int
  81. rstat( path, buf )
  82. char *path;
  83. struct stat *buf;
  84. {
  85.      char *p;
  86.      p = fixfile(path);
  87.      if (p != NULL)
  88.       return( stat( p, buf ) );
  89.      return(-1);    /* failed */
  90. }
  91.  
  92.  
  93. DIR *
  94. ropendir( dirname )
  95. char *dirname;
  96. {
  97.      char *p;
  98.      p = fixfile(dirname);
  99.      if (p != NULL)
  100.       return( opendir( p ) );
  101.      return(NULL);    /* failed */
  102. }
  103.  
  104.  
  105. /*
  106.  * Restricted chdir.
  107.  * 
  108.  * Change to Data_Dir first if it's an absolute path, 
  109.  * then do a relative chdir from there....
  110.  */
  111.  
  112. int
  113. rchdir( path )
  114. char *path;
  115. {
  116.      char *p;
  117.      p = fixfile(path);
  118.      if (p != NULL) {
  119.       if (*p == '/') {
  120.            if (strlen(p) != 1) {
  121.             chdir(Data_Dir);
  122.             return(chdir(p+1));
  123.            } else
  124.             return(chdir(Data_Dir));
  125.       } 
  126.       else
  127.            return( chdir( p ) );
  128.      }
  129.      else
  130.       return(-1);    /* failed */
  131. }
  132.  
  133.  
  134. int
  135. uopen( path, flags, mode )
  136. char *path;
  137. int flags, mode;
  138. {
  139.      return( open( path, flags, mode ) );
  140. }
  141.  
  142.  
  143. FILE *
  144. ufopen( filename, type )
  145. char *filename, *type;
  146. {
  147.      return( fopen( filename, type ) );
  148. }
  149.  
  150.  
  151. int
  152. ustat( path, buf )
  153. char *path;
  154. struct stat *buf;
  155. {
  156.      return( stat( path, buf ) );
  157. }
  158.  
  159.  
  160. DIR *
  161. uopendir( dirname )
  162. char *dirname;
  163. {
  164.      return( opendir( dirname ) );
  165. }
  166.  
  167.  
  168. int
  169. uchdir( path )
  170. char *path;
  171. {
  172.      return( chdir( path ) );
  173. }
  174.  
  175.  
  176. /* Make sure the pathname they gave us is safe and secure for use */
  177.  
  178. char *
  179. fixfile(name)
  180. char *name;
  181. {
  182.      static char newpathbuf[MAXPATHLEN];
  183.      char *newpath;
  184.  
  185.      newpath = &newpathbuf[0];
  186.  
  187.      /* set errno to EPERM in case we reject the request */
  188.      errno = EPERM;
  189.  
  190.      /*
  191.      ** rip any .. or . entries out, so they can't sneak up out of
  192.      ** the gopher directory.  Need to use dedot2() so we don't clobber
  193.      ** the string they sent us originally.
  194.      */
  195.      dedot2(name,newpath);
  196.      while ( *newpath == '/' )    /* make it relative path */
  197.       newpath++;
  198.      if ( *newpath == '\0' )    /* nothing left - it was "/" */
  199.       newpath = ".";
  200.      return( newpath );
  201. }
  202.